<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>通过 shell 执行命令并将完整的输出以字符串的方式返回</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.proc-terminate.html">« proc_terminate</a></li>
      <li style="float: right;"><a href="function.system.html">system »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.exec.html">程序执行函数</a></li>
    <li>通过 shell 执行命令并将完整的输出以字符串的方式返回</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.shell-exec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">shell_exec</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">shell_exec</span> &mdash; <span class="dc-title">通过 shell 执行命令并将完整的输出以字符串的方式返回</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.shell-exec-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>shell_exec</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$command</code></span>): <span class="type"><span class="type">string</span>|<span class="type"><span class="type false">false</span></span>|<span class="type">null</span></span></div>

  <p class="para rdfs-comment">
   本函数同<a href="language.operators.execution.html" class="link">执行运算符</a>。
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    在 Windows 上，底层管道以文本模式打开，这可能导致函数无法进行二进制输出。考虑使用
    <span class="function"><a href="function.popen.html" class="function">popen()</a></span> 避免这种情况。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.shell-exec-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">command</code></dt>

     <dd>

      <p class="para">
       要执行的命令。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.shell-exec-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="type">string</span> 包含已执行命令的输出，如果无法建立管道，则为
   <strong><code>false</code></strong>，如果发生错误或者命令不产生输出则为 <strong><code>null</code></strong>
  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <p class="para">
    当进程执行过程中发生错误，或者进程不产生输出的情况下，都会返回
    <strong><code>null</code></strong>。使用本函数无法检测执行是否成功。当需要访问程序退出代码时，应使用
    <span class="function"><a href="function.exec.html" class="function">exec()</a></span>。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 errors" id="refsect1-function.shell-exec-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   无法建立管道时会产生 <strong><code>E_WARNING</code></strong> 级别的错误。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.shell-exec-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3312">
    <p><strong>示例 #1 <span class="function"><strong>shell_exec()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$output&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">shell_exec</span><span style="color: #007700">(</span><span style="color: #DD0000">'ls&nbsp;-lart'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"&lt;pre&gt;</span><span style="color: #0000BB">$output</span><span style="color: #DD0000">&lt;/pre&gt;"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.shell-exec-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.exec.html" class="function" rel="rdfs-seeAlso">exec()</a> - 执行一个外部程序</span></li>
    <li class="member"><span class="function"><a href="function.escapeshellcmd.html" class="function" rel="rdfs-seeAlso">escapeshellcmd()</a> - shell 元字符转义</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>